package com.servlet;

import com.Impl.RootLogin;
import com.entity.Root;
import jdk.nashorn.internal.ir.CallNode;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Parameter;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        response.setContentType("text/html;charset=utf-8");
//        用户输入的验证码
        String inputVcode = request.getParameter("inputVcode");
        HttpSession sessionCode = request.getSession();
//        系统生成的验证码
        String code = (String) sessionCode.getAttribute("code");
        System.out.println("用户输入的code"+inputVcode);
        System.out.println("系统生成的code"+code);
        RootLogin rootLogin = new RootLogin();
        Root root = rootLogin.queryForRoot(username);
        if (code.equals(inputVcode)){
//            如果能通过username查询到管理员信息
            if (root!=null){
//                如果密码正确
                if (root.getPassword().equals(password)){
                    HttpSession session = request.getSession();
                    session.setAttribute("root", root);
                    response.getWriter().write("loginSuccess");
                }else {
//                    如果密码错误
                    response.getWriter().write("passwordWrong");
                }
            }else {
                response.getWriter().write("noUser");
            }
        }else {
            response.getWriter().write("inputVcodeWrong");
        }
    }
}
